extends Node


onready var wall_scene:PackedScene = preload("res://wall.tscn")
onready var move_body: KinematicBody2D = $Player

export var p_speed:int = 300

var collision = null
var level = 1


func _ready():
	build_level()
	$Layer1/Menu/GameButton.get_popup().connect(
		"index_pressed", self, "_gamebutton_idx_pressed")
	$Layer1/Menu/HelpButton.get_popup().connect("index_pressed", self, "_helpbutton_idx_pressed")

func _physics_process(delta):
	if Input.is_action_pressed("key_up") or\
	Input.is_action_pressed("joy_up"):
		collision = move_body.move_and_collide(
			Vector2(0, -p_speed * delta))
	elif Input.is_action_pressed("key_down") or\
	Input.is_action_pressed("joy_down"):
		collision = move_body.move_and_collide(
			Vector2(0, p_speed * delta))
	elif Input.is_action_pressed("key_left") or\
	Input.is_action_pressed("joy_left"):
		collision = move_body.move_and_collide(
			Vector2(-p_speed * delta, 0))
	elif Input.is_action_pressed("key_right") or\
	Input.is_action_pressed("joy_right"):
		collision = move_body.move_and_collide(
			Vector2(p_speed * delta, 0))
	
#	if collision and (
#	 Input.is_action_pressed("key_action") or\
#	 Input.is_action_pressed("joy_action")):
#		var c: StaticBody2D = collision.collider
#		if c.name != "MenuBody":
#			if Input.is_action_pressed("key_up") or\
#			Input.is_action_pressed("joy_up"):
#				c.position.y -= (p_speed +40 ) * delta
#			elif Input.is_action_pressed("key_down") or\
#			Input.is_action_pressed("joy_down"):
#				c.position.y += p_speed * delta
#			#elif Input.is_action_pressed("key_left") or\
#			#Input.is_action_pressed("joy_left"):
#			c.position.x -= p_speed * delta
##			elif Input.is_action_pressed("key_right") or\
##			Input.is_action_pressed("joy_right"):
##				c.position.x += p_speed * delta

func _input(event):
	if collision and (
	 event.is_action_pressed("key_action") or\
	 event.is_action_pressed("joy_action")):
		if collision.collider.name != "BorderLine" and\
		   collision.collider.name.find("Wall") == -1:
			var k = KinematicBody2D.new()
			k.name = "PlayerBox"
			k.collision_layer = 2
			k.position = move_body.position
			var player_shape = CollisionShape2D.new()
			player_shape.name = "PlayerShape"
			var player_rect = RectangleShape2D.new()
			player_rect.extents = Vector2(32, 32)
			player_shape.shape = player_rect
			k.add_child(player_shape)
			var box_shape = CollisionShape2D.new()
			box_shape.name = "BoxShape"
			box_shape.position = $Box.position -\
								 $Player.position
			var box_rect = RectangleShape2D.new()
			box_rect.extents = Vector2(32, 32)
			box_shape.shape = box_rect
			k.add_child(box_shape)
			var player_sp = Sprite.new()
			player_sp.texture = load("res://icon.png")
			var box_sp = Sprite.new()
			box_sp.texture = load("res://box_1.png")
			box_sp.position = $Box.position -\
							  $Player.position
			k.add_child(player_sp)
			k.add_child(box_sp)
			$Player.hide()
			$Player.collision_layer = 2
			$Box.hide()
			$Box.collision_layer = 2
			add_child(k)
			move_body = k
	if move_body.name == "PlayerBox" and(
	 event.is_action_released("key_action") or\
	 event.is_action_released("joy_action")):
		var k = move_body
		var pos = k.position
		$Player.position = pos
		$Box.position =\
			pos + k.get_node("BoxShape").position
		k.hide()
		move_body = $Player
		k.collision_mask = 2
		k.queue_free()
		$Player.show()
		$Player.collision_layer = 1
		$Box.show()
		$Box.collision_layer = 1


func build_level():
	$Layer1/Over.hide()
	match level:
		1:
			var map = [
				[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,1,9,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,2,0,0,0,0,0,0,0,0,0,0,0,0,3,1],
				[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
				[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
			]
			for i in range(9):
				for j in range(16):
					match map[i][j]:
						1:
							var wall = wall_scene.instance()
							wall.position.x = j * 64
							wall.position.y = i * 64 + 24
							add_child(wall)
						2:
							$Player.position.x = j * 64 + 32
							$Player.position.y = i * 64 + 56
						3:
							$Box.position.x = j * 64 - 32
							$Box.position.y = i * 64 + 56
						9:
							$End.position.x = j * 64 + 32
							$End.position.y = i * 64 + 56


func _on_End_body_shape_entered(body_id, body, body_shape, local_shape):
	if body_shape:
		$Layer1/Over.show()
		get_tree().paused = true

func _gamebutton_idx_pressed(idx: int):
	match idx:
		0:
			get_tree().paused = false
			if get_node("PlayerBox"):
				move_body = $Player
				get_node("PlayerBox").queue_free()
				$Player.show()
				$Player.collision_layer = 1
				$Box.show()
				$Box.collision_layer = 1
			build_level()

func _helpbutton_idx_pressed(idx: int):
	$Layer1/Help.show()
	get_tree().paused = true

func _on_help_exit_pressed():
	get_tree().paused = false
	$Layer1/Help.hide()
